/*
 * Copyright (c) 2008, Christoph Hellwig
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#include "xfs.h"
#include "xfs_format.h"
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
#include "xfs_sb.h"
#include "xfs_ag.h"
#include "xfs_mount.h"
#include "xfs_inode.h"
#include "xfs_quota.h"
#include "xfs_trans.h"
#include "xfs_qm.h"
#include <linux/quota.h>


STATIC int
dfs_quota_type(int type)
{
	switch (type) {
	case USRQUOTA:
		return DFS_DQ_USER;
	case GRPQUOTA:
		return DFS_DQ_GROUP;
	default:
		return DFS_DQ_PROJ;
	}
}

STATIC int
dfs_fs_get_xstate(
	struct super_block	*sb,
	struct fs_quota_stat	*fqs)
{
	struct dfs_mount	*mp = DFS_M(sb);

	if (!DFS_IS_QUOTA_RUNNING(mp))
		return -ENOSYS;
	return -dfs_qm_scall_getqstat(mp, fqs);
}

STATIC int
dfs_fs_get_xstatev(
	struct super_block	*sb,
	struct fs_quota_statv	*fqs)
{
	struct dfs_mount	*mp = DFS_M(sb);

	if (!DFS_IS_QUOTA_RUNNING(mp))
		return -ENOSYS;
	return -dfs_qm_scall_getqstatv(mp, fqs);
}

STATIC int
dfs_fs_set_xstate(
	struct super_block	*sb,
	unsigned int		uflags,
	int			op)
{
	struct dfs_mount	*mp = DFS_M(sb);
	unsigned int		flags = 0;

	if (sb->s_flags & MS_RDONLY)
		return -EROFS;
	if (op != Q_XQUOTARM && !DFS_IS_QUOTA_RUNNING(mp))
		return -ENOSYS;

	if (uflags & FS_QUOTA_UDQ_ACCT)
		flags |= DFS_UQUOTA_ACCT;
	if (uflags & FS_QUOTA_PDQ_ACCT)
		flags |= DFS_PQUOTA_ACCT;
	if (uflags & FS_QUOTA_GDQ_ACCT)
		flags |= DFS_GQUOTA_ACCT;
	if (uflags & FS_QUOTA_UDQ_ENFD)
		flags |= DFS_UQUOTA_ENFD;
	if (uflags & FS_QUOTA_GDQ_ENFD)
		flags |= DFS_GQUOTA_ENFD;
	if (uflags & FS_QUOTA_PDQ_ENFD)
		flags |= DFS_PQUOTA_ENFD;

	switch (op) {
	case Q_XQUOTAON:
		return -dfs_qm_scall_quotaon(mp, flags);
	case Q_XQUOTAOFF:
		if (!DFS_IS_QUOTA_ON(mp))
			return -EINVAL;
		return -dfs_qm_scall_quotaoff(mp, flags);
	case Q_XQUOTARM:
		if (DFS_IS_QUOTA_ON(mp))
			return -EINVAL;
		return -dfs_qm_scall_trunc_qfiles(mp, flags);
	}

	return -EINVAL;
}

STATIC int
dfs_fs_get_dqblk(
	struct super_block	*sb,
	struct kqid		qid,
	struct fs_disk_quota	*fdq)
{
	struct dfs_mount	*mp = DFS_M(sb);

	if (!DFS_IS_QUOTA_RUNNING(mp))
		return -ENOSYS;
	if (!DFS_IS_QUOTA_ON(mp))
		return -ESRCH;

	return -dfs_qm_scall_getquota(mp, from_kqid(&init_user_ns, qid),
				      dfs_quota_type(qid.type), fdq);
}

STATIC int
dfs_fs_set_dqblk(
	struct super_block	*sb,
	struct kqid		qid,
	struct fs_disk_quota	*fdq)
{
	struct dfs_mount	*mp = DFS_M(sb);

	if (sb->s_flags & MS_RDONLY)
		return -EROFS;
	if (!DFS_IS_QUOTA_RUNNING(mp))
		return -ENOSYS;
	if (!DFS_IS_QUOTA_ON(mp))
		return -ESRCH;

	return -dfs_qm_scall_setqlim(mp, from_kqid(&init_user_ns, qid),
				     dfs_quota_type(qid.type), fdq);
}

const struct quotactl_ops dfs_quotactl_operations = {
	.get_xstatev		= dfs_fs_get_xstatev,
	.get_xstate		= dfs_fs_get_xstate,
	.set_xstate		= dfs_fs_set_xstate,
	.get_dqblk		= dfs_fs_get_dqblk,
	.set_dqblk		= dfs_fs_set_dqblk,
};
